
chains = {}
for i in range(1, 1000000):
    length = 1
    n = i 
    while n != 1:
        if n % 2 == 0:
            n = n / 2
        else:
            n = 3*n + 1

        if n in chains:
            length += chains[n]
            break
        else:
            length += 1
    chains[i] = length

print max(chains, key=chains.get)

